import { Component, OnInit, OnDestroy, EventEmitter } from "@angular/core";
import { Observable } from "rxjs/Observable";
import { Subject } from "rxjs/Subject";
import { Subscription } from "rxjs/Subscription";
import { StockBusinessHistory } from "./stock-business-history-replay";
import { Headers } from "@angular/http";
import { CmHttp } from "http-module";
import { UserToken, DEFAULT_VERIFY, OPERATE_BUY, OPERATE_SELL } from "vtrade";
import { HsClientService } from "./../hs-client.service";


const TABLE_HEADERS = [
  {
    "label": "交易日期",
    "key": "init_date"
  },
  {
    "label": "成交时间",
    "key": "business_time"
  },
  {
    "label": "委托编号",
    "key": "entrust_no"
  },
  {
    "label": "证券名称",
    "key": "stock_name"
  },
  {
    "label": "证券代码",
    "key": "stock_code"
  },
  {
    "label": "买卖方向",
    "key": "entrust_bs"
  },
  {
    "label": "成交价格",
    "key": "business_price"
  },
  {
    "label": "成交数量",
    "key": "occur_amount",
  },
  {
    "label": "成交金额",
    "key": "business_balance",
  },
  {
    "label": "手续费",
    "key": "commission_charge",
  },
  {
    "label": "印花税",
    "key": "stamp_tax",
  },
  {
    "label": "过户费",
    "key": "transfer_fee",
  },
  {
    "label": "其他费用",
    "key": "other_fee",
  },
  {
    "label": "清算费用",
    "key": "clear_fee"
  }
];

@Component({
  // moduleId: module.id,
  selector: "stock-business-history",
  templateUrl: "stock-business-history.component.html",
  styleUrls: [
    "stock-business-history.component.less"
  ],
  inputs: [
    "updateSource"
  ],
  outputs: [
    "entryClick"
  ]
})
export class StockBusinessHistoryComponent implements OnInit, OnDestroy {
  mainSubscription: Subscription;
  tableHeaders = TABLE_HEADERS;
  updateSource: Subject<any>;
  dataSource: Observable<StockBusinessHistory[]>;
  dataList: StockBusinessHistory[];
  positionClick = new EventEmitter();

  constructor(private hsClient: HsClientService) { }

  ngOnInit() {
    !this.updateSource ? this.updateSource = new Subject() : null;
    console.debug("StockBusiness UpdateSource: ", this.updateSource);
    this.dataSource =
      this.updateSource
        .switchMap((date) => {
          console.debug("Date: ", date);
          return this.hsClient.queryBusinessHis(date)
        })
    this.dataSource.subscribe(
      res => this.dataList = res,
      err => this.dataList = []
    );
  }

  ngOnDestroy(){
    if (this.mainSubscription) {
      this.mainSubscription.unsubscribe();
    }
  }

  trackBySymbol(symbol: StockBusinessHistory) {
    return symbol.stock_code;
  }

  handleOperate(entry: StockBusinessHistory, isSell?: boolean) {

  }

  getEntryClass(entry: StockBusinessHistory) {
    return {
      // "color-red": entry.income_balance >= 0,
      // "color-green": entry.income_balance < 0
    };
  }

}